home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
8_6.lha
/
8_6
/
in.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
5KB
|
367 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*ident "@(#)cfront:lib/stream/in.c 1.8" */
*
C++ stream i/o source
in.c
/
include <ctype.h>
include <stream.h>
/ #include <common.h>
* predefined whitespace */
hitespace WS;
/ inline
oid eatwhite (istream& is)
if (is.tied_to) is.tied_to->flush();
register streambuf *nbp = is.bp;
register c = nbp->sgetc();
while (isspace(c&0377)) c = nbp->snextc();
if (c == EOF) is.state |= _eof;
stream& istream::operator>>(whitespace&)
register streambuf *nbp = bp;
if (state) return *this;
if (tied_to) tied_to->flush();
register c = nbp->sgetc();
while (isspace(c)) c = nbp->snextc();
if (c == EOF) state |= _eof;
return *this;
stream& istream::operator>>(register char& s)
*
reads characters NOT very small integers
*/
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
register c = bp->sgetc();
if (c == EOF) {
state |= _fail|_eof;
} else {
s = c;
bp->stossc();
}
return *this;
stream& istream::operator>>(register char* s)
register streambuf *nbp = bp;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
/* get string */
register c = nbp->sgetc();
if (c == EOF) state |= _fail;
while (!isspace(c) && c != EOF) {
*s++ = c;
c = nbp->snextc();
}
*s = '\0';
if (c == EOF) state |= _eof;
return *this;
stream& istream::operator>>(long& i)
register c;
register ii = 0;
register streambuf *nbp = bp;
int neg = 0;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
switch (c = nbp->sgetc()) {
case '-':
case '+':
neg = c;
c = nbp->snextc();
break;
case EOF:
state |= _fail;
}
if (isdigit(c)) {
do {
ii = ii*10+c-'0';
} while (isdigit(c=nbp->snextc()));
i = (neg=='-') ? -ii : ii;
} else
state |= _fail;
if (c == EOF) state |= _eof;
return *this;
stream& istream::operator>>(int& i)
long l;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ( *this>>l ) {
i = (int)l;
}
return *this;
stream& istream::operator>>(short& i)
long l;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ( *this>>l ) {
i = (short)l;
}
return *this;
stream& istream::operator>>(double& d)
*
{+|-} d* {.} d* { e|E {+|-} d+ }
except that
- a dot must be pre- or succeded by at least one digit
- an exponent must be preseded by at least one digit
/
register c = 0;
char buf[256];
register char* p = buf;
register streambuf* nbp = bp;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
/* get the sign */
switch (c = nbp->sgetc()) {
case EOF:
state = _eof|_fail;
return *this;
case '-':
case '+':
*p++ = c;
c = bp->snextc();
}
/* get integral part */
while (isdigit(c)) {
*p++ = c;
c = bp->snextc();
}
/* get fraction */
if (c == '.') {
do {
*p++ = c;
c = bp->snextc();
} while (isdigit(c));
}
/* get exponent */
if (c == 'e' || c == 'E') {
*p++ = c;
switch (c = nbp->snextc()) {
case EOF:
state = _eof|_fail;
return *this;
case '-':
case '+':
*p++ = c;
c = bp->snextc();
}
while (isdigit(c)) {
*p++ = c;
c = bp->snextc();
}
}
*p = 0;
d = atof(buf);
if (c == EOF) state |= _eof;
return *this;
stream& istream::operator>>(float& f)
double d;
if (skipws)
eatwhite(*this);
else if (tied_to)
tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ( *this>>d ) {
f = d;
}
return *this;
stream& istream::get(
register char* s, /* character array to read into */
register int len, /* size of character array */
register char term /* character that terminates input */
{
register c;
register streambuf *nbp = bp;
if (tied_to) tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ((c = bp->sgetc()) == EOF) {
state |= _fail | _eof;
return *this;
}
while (c != term && c != EOF && len > 1) {
*s++ = c;
c = nbp->snextc();
len--;
}
*s = '\0';
if (c == EOF) state |= _eof;
return *this;
stream& istream::putback(register char c)
bp->sputbackc(c);
return *this;
stream& istream::get(
register streambuf &s, /* streambuf to input to */
register char term /* termination character */
{
register c;
register streambuf *nbp = bp;
if (tied_to) tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ((c = bp->sgetc()) == EOF) {
state |= _fail | _eof;
return *this;
}
while (c != term && c != EOF) {
if (s.sputc(c) == EOF) break;
c = nbp->snextc();
}
if (c == EOF) state |= _eof;
return *this;
stream& istream::operator>>(register streambuf &s)
register c;
register streambuf *nbp = bp;
if (tied_to) tied_to->flush();
if (state) {
state |= _fail;
return *this;
}
if ((c = bp->sgetc()) == EOF) {
state |= _fail | _eof;
return *this;
}
while (c != EOF) {
if (s.sputc(c) == EOF) break;
c = nbp->snextc();
}
if (c == EOF) state |= _eof;
return *this;
/istream& istream::operator>>(common& p)
/{
/ if (skipws)
/ eatwhite(*this);
/ else if (tied_to)
/ tied_to->flush();
/
/ return p.read(*this);
/}